﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Runtime.Serialization;

namespace WZ.Utils.WebHttp
{
    [Serializable]
    public class HttpRecordCollection : ICollection<HttpRecord>, ICloneable
    {
        private List<HttpRecord> _list;
        public HttpRecordCollection()
        {
            _list = new List<HttpRecord>();
        }
        #region ICollection<HttpRecord> 成员

        public void Add(HttpRecord item)
        {
            _list.Add(item);
        }

        public void Clear()
        {
            _list.Clear();
        }

        public bool Contains(HttpRecord item)
        {
            return _list.Contains(item);
        }

        public void CopyTo(HttpRecord[] array, int arrayIndex)
        {
            _list.CopyTo(array, arrayIndex);
        }

        public int Count
        {
            get { return _list.Count; }
        }

        public bool IsReadOnly
        {
            get { return true; }
        }

        public bool Remove(HttpRecord item)
        {
            return _list.Remove(item);
        }

        #endregion

        #region IEnumerable<HttpRecord> 成员

        public IEnumerator<HttpRecord> GetEnumerator()
        {
            return _list.GetEnumerator();
        }

        #endregion

        #region IEnumerable 成员

        IEnumerator IEnumerable.GetEnumerator()
        {
            return _list.GetEnumerator();
        }

        #endregion

        public object Clone()
        {
            HttpRecordCollection returnValue = new HttpRecordCollection();
            foreach (HttpRecord record in _list)
            {
                HttpRecord newRecord = new HttpRecord();
                newRecord.Link = record.Link;
                newRecord.Method = record.Method;
                foreach (HttpParamater paramater in record.Paramaters)
                {
                    HttpParamater newParamater = new HttpParamater();
                    newParamater.Key = paramater.Key;
                    newParamater.Value = paramater.Value;
                    newRecord.Paramaters.Add(newParamater);
                }
                newRecord.RecordTime = record.RecordTime;
                newRecord.RequestHeaders.Add(record.RequestHeaders);
                newRecord.ResponseContent = record.ResponseContent;
                newRecord.ResponseHeaders.Add(record.ResponseHeaders);

                returnValue.Add(newRecord);
            }

            return returnValue;
        }
    }
}
